/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function (nums) {
  let arr = [],
    res = [],
    visited = new Array(nums.length).fill(0)
  bc(0)

  function bc() {
    if (arr.length == nums.length) {
      res.push([...arr])
      return
    }

    for (let i = 0; i < nums.length; i++) {
      if(visited[i])continue
      visited[i]=1
      arr.push(nums[i])
      bc(i + 1)
      visited[i]=0
      arr.pop()
    }
  }
  return res
}

permute([1, 2, 3])
